package com.hfjyz.registration.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.registration.command.PrintInfoCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Depart;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.model.Office;
import com.hfjyz.registration.object.PrintInfoBean;
import com.hfjyz.registration.service.PrintInfoService;

public class PrintInfoController extends SimpleFormController {
    private PrintInfoService printInfoService;

    public PrintInfoService getPrintInfoService() {
        return printInfoService;
    }

    public void setPrintInfoService(PrintInfoService printInfoService) {
        this.printInfoService = printInfoService;
    }

    @Override
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
        PrintInfoCommand cmd = (PrintInfoCommand) command;
        List<PrintInfoBean> printInfos = printInfoService.getPrintInfos(cmd);
        
        Exam exam = (Exam) request.getSession().getAttribute(
                ExamContants.SESSION_KEY_EXAM_INFO);
        cmd.setExamId(exam.getId());

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("printInfos", printInfos);
        initModel(model, exam);

        return model;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
            BindException errors) throws Exception {
        PrintInfoCommand cmd = (PrintInfoCommand) command;
        Exam exam = (Exam) request.getSession().getAttribute(
                ExamContants.SESSION_KEY_EXAM_INFO);
        cmd.setExamId(exam.getId());

        List<PrintInfoBean> printInfos = printInfoService.getPrintInfos(cmd);
        Map model = errors.getModel();
        model.put("printInfos", printInfos);
        initModel(model, exam);
        
        return new ModelAndView(getSuccessView(), model);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void initModel(Map model, Exam exam) throws Exception {

        List<Depart> deptList = printInfoService.listDepart(exam);

        Depart dept = new Depart();
        dept.setId(-1);
        dept.setName("");
        deptList.add(0, dept);
        model.put("departs", deptList);

        List<Office> officeList = printInfoService.listOffice(exam);
        Office office = new Office();
        office.setId(-1);
        office.setName("");
        office.setCode("");
        officeList.add(0, office);
        model.put("offices", officeList);
    }
}
